<?php
include_once 'admin_global.php';            //加载后台统一调用类文件
$db->Get_user_shell_check($uid, $shell);    //检测用户是否登录
$db->Get_user_type_check($uid,$shell); //检测用户权限

//获取表单数据，准备写入数据库
if(isset($_POST['addAdmin'])){
    //验证用户名是否存在，保证用户名的唯一性
    $userName=trim($_POST['userName']);
    $query=$db->select("admin","*","userName='$userName'");
    $num=$db->db_num_rows();
    if($num==0){
        //获取表单数据
        $userPassword=sha1(trim($_POST['userPassword']));
        $userEmail=trim($_POST['userEmail']);
        $userType=$_POST['userType'];
        $userSex=$_POST['userSex'];
        if(!empty($_POST['userEnabled'])){
            $userEnabled=1;
        }else{
            $userEnabled=0;
        }
        $userRemark=trim($_POST['userRemak']);
        $userCreateTime=time();
        //写入数据表
        $rows=$db->insert("admin","userID,userName,userPassword,userEmail,userSex,userType,userCreateTime,userEnabled,userRemark","null,'$userName','$userPassword','$userEmail','$userSex','$userType',$userCreateTime,$userEnabled,'$userRemark'");
        if($rows==1){
            echo "<script>alert('用户添加成功！');location.href='listAdmin.php';</script>";
        }else{
            echo "<script>alert('用户添加失败');history.go(-1);</script>";
        }
    }else{
        echo "<script>alert('该用户已存在！');history.go(-1);</script>";
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>添加管理员</title>
        <link rel="stylesheet" href="css/right.css" type="text/css" />
        <script type="text/javascript" src="js/jquery-1.12.4.min.js" ></script>
        <script type="text/javascript" src="js/jquery.validate-1.13.1.js" ></script>
    </head>
    <script>
        $(function(){
            $("#form1").validate({
                rules:{
                    userName:{
                        required:true,
                        rangelength:[2,16]
                    },
                    userPassword:{
                        required:true,
                        rangelength:[6,16] 
                    },
                    ReuserPassword:{
                        equalTo:"#userPassword"
                    },
                    userEmail:{
                        email:true
                    }
                },
                messages:{
                    userName:{
                        required:"请输入用户名称！",
                        rangelength:"用户名长度在2~16个字符内！"
                    },
                    userPassword:{
                        required:"请输入密码！",
                        rangelength:"密码长度在6~16个字符内！" 
                    },
                    ReuserPassword:{
                        equalTo:"两次输入的密码不一致！"
                    },
                    userEmail:{
                        email:"请输入正确电子邮件格式输入！"
                    }
                }
            });
        })
    </script>
    <body>
        <div id="right_main">
            <div class="right_content">
                <div class="right_top_menu">
                    <h3>
                        您当前的位置：后台 > 管理设置 > 添加管理员
                    </h3>
                </div>
                <div class="right_center_content">
                    <form id="form1" method="post" action="">
                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                            <tr>
                                <td valign="top" >
                                    <div class="txl_search"></div>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%"  class="sy_table">
                                        <tr>
                                            <th colspan="2" >添加管理员</th>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">管理员名称：</td>
                                            <td width="65%"><input type="text" name="userName"  class="int_text" value="" size="40" /> </td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">管理员密码：</td>
                                            <td width="65%"><input type="password" name="userPassword" id="userPassword" class="int_text" value="" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">重复密码：</td>
                                            <td width="65%"><input type="password" name="ReuserPassword"  class="int_text" value="" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">管理员邮箱：</td>
                                            <td width="65%"><input type="text" name="userEmail"  class="int_text" value="" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">管理员类型：</td>
                                            <td width="65%">
                                                <select name="userType">
                                                    <option  selected value="超级管理员">超级管理员</option> 
                                                    <option  value="普通管理员">普通管理员</option> 
                                                </select>
                                            &nbsp;&nbsp;* 超级管理员拥有后台管理的全部权限，普通管理员只有部分权限！</td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">性别：</td>
                                            <td width="65%">
                                                <select name="userSex">
                                                    <option  selected value="男">男</option> 
                                                    <option  value="女">女</option> 
                                                    <option value="保密">保密</option> 
                                                </select>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">是否可用：</td>
                                            <td width="65%"><input type="checkbox" name="userEnabled"  checked="checked" class="int_text" value="6" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">备注：</td>
                                            <td width="65%"><input type="text" name="userRemark"  class="int_text" value="" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td colspan="2" align="center" height='30'>
                                                <input type="submit" class="btn_ipt01" name="addAdmin" value=" 添加管理员 "/>
                                            </td>  
                                        </tr>
                                    </table>
                                </td>
                            </tr>   
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
